<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
		<%
		//第一步: 获取参数
		request.setCharacterEncoding("utf-8");
		String sno = request.getParameter("sno");
		String sdep = request.getParameter("sdep");
		String ssex = request.getParameter("ssex");
		String sage = request.getParameter("sage");
		String sname = request.getParameter("sname");
		
		//第二步: 连接数据库
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/st?useUnicode=true&characterEncoding=utf-8";
			String user = "root";
			String pass = "123456";
			Connection con = DriverManager.getConnection(url, user, pass);
			
		//第三步: 调用prepareStatement方法,对数据库进行增加数据的操作
	PreparedStatement ps = 	con.prepareStatement("insert into student2(sno,sdep,ssex,sage,sname)values(?,?,?,?,?)");
		ps.setString(1, sno);
		ps.setString(2, sdep);
		ps.setString(3,ssex);
		ps.setInt(4, Integer.parseInt(sage));
		ps.setString(5, sname);
		ps.executeUpdate();
		
		//第四步:关闭数据源
		ps.close();
		con.close();
		
		//第五步: 增加数据执行完成后,跳转回showinfo1.jsp页面
		request.getRequestDispatcher("showinfo1.jsp").forward(request, response);
		
		//第六步: 避免异常情况的出现
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		%>
</body>
</html>